using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class UserSecurityDal : UserDal
    {
        public UserSecurityDal() : base() { }

        public new enum Fields
        {
            Password,
            Salt
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            base.ReadRecord(table, reader);

            GetReaderValue<string>(Fields.Password, reader, table);
            GetReaderValue<int>(Fields.Salt, reader, table);
        }

        /*public new Hashtable[] GetAll()
        {
            SqlCommand cmd = CreateCommand("GetAll");
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllPaged(string sortExpression, int startRecord, int pageSize, ref int totalRecords)
        {
            SqlCommand cmd = CreateCommand("GetAllPaged");
            AddInputParameter(cmd, "Sort", sortExpression, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "StartIndex", startRecord, SqlDbType.Int, 255);
            AddInputParameter(cmd, "PageSize", pageSize, SqlDbType.Int, 255);
            AddOutputParameter(cmd, "TotalRecords", SqlDbType.Int, 255);
            Hashtable[] tables = ExecuteGetCollection(cmd);
            totalRecords = GetOutputValue<int>(cmd, "TotalRecords");
            return tables;
        }

        public Hashtable[] GetAllByEmailAddressPaged(string emailAddress, string sortExpression, int startRecord, int pageSize, ref int totalRecords)
        {
            SqlCommand cmd = CreateCommand("GetAllByEmailAddressPaged");
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 1000);
            AddInputParameter(cmd, "Sort", sortExpression, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "StartIndex", startRecord, SqlDbType.Int, 255);
            AddInputParameter(cmd, "PageSize", pageSize, SqlDbType.Int, 255);
            AddOutputParameter(cmd, "TotalRecords", SqlDbType.Int, 255);
            Hashtable[] tables = ExecuteGetCollection(cmd);
            totalRecords = GetOutputValue<int>(cmd, "TotalRecords");
            return tables;
        }

        public Hashtable[] GetAllByUsernamePaged(string username, string sortExpression, int startRecord, int pageSize, ref int totalRecords)
        {
            SqlCommand cmd = CreateCommand("GetAllByUsernamePaged");
            AddInputParameter(cmd, "Username", username, SqlDbType.VarChar, 1000);
            AddInputParameter(cmd, "Sort", sortExpression, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "StartIndex", startRecord, SqlDbType.Int, 255);
            AddInputParameter(cmd, "PageSize", pageSize, SqlDbType.Int, 255);
            AddOutputParameter(cmd, "TotalRecords", SqlDbType.Int, 255);
            Hashtable[] tables = ExecuteGetCollection(cmd);
            totalRecords = GetOutputValue<int>(cmd, "TotalRecords");
            return tables;
        }
        */

        public new Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.VarChar, 50);
            return ExecuteGetItem(cmd);
        }
        
        public new Hashtable GetByUserName(string userName, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetByUserName");
            AddInputParameter(cmd, "UserName", userName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }
        /*
        public int CountOnline(int minutes)
        {
            SqlCommand cmd = CreateCommand("CountOnline");
            AddInputParameter(cmd, "Minutes", minutes, SqlDbType.Int);
            return ExecuteScalar<int>(cmd);
        }*/

        /*public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }*/

        public void Update(ref int id, int siteId, string userName, string firstName, string lastName, DateTime dateOfBirth, string emailAddress, byte gender, string address1, string address2, string town, string postCode, string mobileTel, DateTime created, string password, int salt, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "UserName", userName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "FirstName", firstName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "LastName", lastName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "DateOfBirth", dateOfBirth, SqlDbType.DateTime);
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "Gender", gender, SqlDbType.TinyInt);
            AddInputParameter(cmd, "Address1", address1, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "Address2", address2, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "Town", town, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "PostCode", postCode, SqlDbType.VarChar, 20);
            AddInputParameter(cmd, "MobileTel", mobileTel, SqlDbType.VarChar, 20);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "Password", password, SqlDbType.VarChar);
            AddInputParameter(cmd, "Salt", salt, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void CreateLoginKey(Guid key, int userId, DateTime loggedIn, bool perminant)
        {
            SqlCommand cmd = CreateCommand("CreateLoginKey");
            AddInputParameter(cmd, "Key", key, SqlDbType.UniqueIdentifier);
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "LoggedIn", loggedIn, SqlDbType.DateTime);
            AddInputParameter(cmd, "Perminant", perminant, SqlDbType.Bit);
            base.ExecuteCommand(cmd);
        }
    }
}
